#include <sstream>

#include <unistd.h>
#include <sys/stat.h>
#include <time.h>

#include "fileprintdetail.h"
#include "rectype.h"
#include "recbase.h"
#include "util.h"
#include "nmfield.h"
#include "nmfield.cpp"

#include "cnfield.h"

#include "nmarrfield.h"
#include "nmarrfield.cpp"

#include "stdffield.h"
#include "stdffield.cpp"

#include "far.h"
#include "atr.h"
#include "mir.h"
#include "mrr.h"
#include "pcr.h"
#include "hbr.h"
#include "sdr.h"
#include "sbr.h"
#include "pmr.h"
#include "pgr.h"
#include "plr.h"
#include "rdr.h"
#include "wir.h"
#include "wrr.h"
#include "wcr.h"
#include "pir.h"
#include "prr.h"
#include "tsr.h"
#include "ptr_xy.h"
#include "bps.h"
#include "eps.h"
#include "gdr.h"
#include "dtr.h"
#include "ftr.h"
#include "mpr.h"

#include "util.h"

using namespace std;
using namespace opsrc;
using namespace opsrc::stdf;

FilePrintDetail::FilePrintDetail()
{
    pfile = NULL;
    m_type = 1;
}

void FilePrintDetail::out(RecBase* rec){
    stdf_rec rt = rec->getStdfRec();
    if(pfile != NULL)
    {
        switch (rt) {

        case ATR:
            {
                Atr* pAtr = dynamic_cast<Atr*>(rec);
                struct tm* t = Util::convertUnixTime2TM(pAtr->getModTime().getData());
                *pfile << asctime(t) << ","
                        << pAtr->getCmdLine().getData() << endl;
            }
            break;

        case BPS:
            {
                Bps* pBps = dynamic_cast<Bps*>(rec);
                *pfile << pBps->getSequenceName().getData() <<","<< pBps->getRecOrder() << endl;
            }
            break;

        case DTR:
            {
                Dtr* pDtr = dynamic_cast<Dtr*>(rec);
                *pfile << pDtr->getTextData().getData() << endl;
            }
            break;

        case EPS:
            {
                Eps* pEps = dynamic_cast<Eps*>(rec);
                *pfile << pEps->getRecOrder() << endl;
            }
            break;

        case FAR:
            {
                Far* pFar = dynamic_cast<Far*>(rec);
                (*pfile) << pFar->getStdfRec() << endl;
            }
            break;
        case FTR:
            {
                Ftr* p = dynamic_cast<Ftr*>(rec);
                (*pfile) << p->getTestNumber().getData() << ","
                        << p->getHeadNumber().getData() << ","
                        << p->getSiteNumber().getData() << ","
                        << convertBoolArraytoStr(p->getTestFlag()) << ","
                        << p->getVectorModulePatternName().getData() << ","
                        << p->getTimeSetName().getData() << ","
                        << p->getCycleCount().getData() << ","
                        << p->getRelativeVectorAddress().getData() << ","
                        << p->getRepeatedVectorCount().getData() << ","
                        << p->getFailPinBitMapLength().getData() << ","
                        << p->getXlogicalDeviceAddressFailure().getData() << ","
                        << p->getYlogicalDeviceAddressFailure().getData() << ","
                        << p->getVectorOffset().getData() << ","
                        << convertIntArraytoStr( p->getPmrArrayData() ) << ","
                        << convertIntArraytoStr( p->getPmrArrayState() ) << ","
                        << convertIntArraytoStr( p->getProgramsStateIndexArray() ) << ","
                        << convertIntArraytoStr( p->getProgramStateDataArray() ) << ","
                        << p->getFailPinBitMapLength().getData() << ","
                        << convertBoolArraytoStr( p->getOptionFlag() ) << ","
                        << p->getTestNumber().getData() << ","
                        << p->getAlarmId().getData() << ","
                        << p->getAdditonalProgramInfo().getData() << ","
                        << p->getAdditionalResultInfo().getData() << ","
                        << p->getPatternGenertorNumber().getData() << ","
                        << p->getEnabledComparatorBitMap().getData() << endl;
            }
            break;

        case GDR:
            {
                //Gdr* pGdr = dynamic_cast<Gdr*>(rec);
                //*pfile<<"To be decide later."<<endl;
            }
            break;
        case HBR: //*//
            {
                Hbr* pHbr = dynamic_cast<Hbr*>(rec);
                //"HBR: lot_id,wafer_id,recseq_no,head_num,
                (*pfile) << lot_id << ","
                        << wafer_id << ","
                        << pHbr->getRecOrder() << ","
                        << pHbr->getHeadNum().getData() << ","
                        //<< pHbr->getSiteNum().getData() << ","
                        //hbin_num,hbin_cnt,hbin_pf,hbin_nam,time_stamp"
                        << pHbr->getHbinNum().getData() << ","
                        << pHbr->getHbinCount().getData() << ","
                        << pHbr->getHbinPassFail().getData() << ","
                        << pHbr->getHbinName().getData()
                        //<< Util::convertDtToMysqlDtFormat(time(0))
                        << endl;
            }
            break;
        case MIR://*//
            {
                //"MIR: lot_id,wafer_id,part_typ,job_nam,node_nam,tstr_typ,setup_t,start_t,"
                Mir* pMir = dynamic_cast<Mir*>(rec);
                (*pfile) << pMir->getLotId().getData() << ","
                        << wafer_id << ","
                        << pMir->getPartType().getData() << ","
                        << pMir->getJobName().getData() << ","
                        << pMir->getNodeName().getData() << ","
                        << pMir->getTesterType().getData() << ","
                        << Util::convertDtToMysqlDtFormat( pMir->getSetupTime().getData()) << ","
                        << Util::convertDtToMysqlDtFormat( pMir->getStartTime().getData()) << ","

                        // "oper_nam,mode_cod,stat_num,sblot_id,test_cod,rtst_cod,job_rev,"
                        << pMir->getOperatorName().getData() << ","
                        << pMir->getModeCode().getData() << ","
                        << pMir->getStartNum().getData() << ","
                        << pMir->getSubLotId().getData() << ","
                        << pMir->getTestCode().getData() << ","
                        << pMir->getRetestCode().getData() << ","
                        << pMir->getJobRevision().getData() << ","

                        // "exec_typ,exec_ver,prot_cod,cmod_cod,burn_tim,tst_temp,user_txt,"
                        << pMir->getExecutionType().getData() << ","
                        << pMir->getExecutiveVersion().getData() << ","
                        << pMir->getProductCode().getData() << ","
                        << pMir->getCmodCode().getData() << ","
                        << pMir->getBurnTime().getData() << ","
                        << pMir->getTestTemperature().getData() << ","
                        << pMir->getUserText().getData() << ","

                        //"aux_file,pkg_typ,famly_id,dat_cod,facil_id,floor_id,proc_id,oper_frq,"
                        << pMir->getAuxilaryFile().getData() << ","
                        << pMir->getPackageType().getData() << ","
                        << pMir->getFamilyId().getData() << ","
                        << pMir->getDateCode().getData() << ","
                        << pMir->getTestFacilityId().getData() << ","
                        << pMir->getTestFloorId().getData() << ","
                        << pMir->getProcessId().getData() << ","
                        << pMir->getOperationFrequency().getData() << ","

                        //"spec_nam,spec_ver,flow_id,setup_id,dsgn_rev,eng_id,rom_cod,serl_num,supr_nam,time_stamp"
                        << pMir->getSpecName().getData() << ","
                        << pMir->getSpecVersion().getData() << ","
                        << pMir->getFlowId().getData() << ","
                        << pMir->getSetupId().getData() << ","
                        << pMir->getDesignId().getData() << ","
                        << pMir->getEngrgId().getData() << ","
                        << pMir->getRomCode().getData() << ","
                        << pMir->getSerialNumber().getData() << ","
                        << pMir->getSupervisorName().getData()
                        //<< Util::convertDtToMysqlDtFormat(time(0))
                        << endl;
            }
            break;

        case MPR:
            {
                Mpr* pMpr = dynamic_cast<Mpr*>(rec);
                (*pfile) << pMpr->getTestNumber().getData() << ","
                        << pMpr->getHeadNumber().getData() << ","
                        << pMpr->getSiteNumber().getData() << ","
                        << convertIntArraytoStr( pMpr->getReturnStatesArray() ) << ","
                        << pMpr->getReturnResultsCount().getData() << ","
                        << pMpr->getUnits().getData() << ","
                        << pMpr->getLowerLimitFormat().getData() << ","
                        << pMpr->getTestUpperLimitValue().getData() << ","
                        << pMpr->getStartingInValue().getData() << ","
                        << pMpr->getStartingIncrementalValue().getData() << ","
                        << pMpr->getInputConditionUnits().getData() << ","
                        << convertUIntArraytoStr(pMpr->getPmrIndexArray() ) << ","
                        << pMpr->getResultFormat().getData() << ","
                        << pMpr->getLowerLimitFormat().getData() << ","
                        << pMpr->getUpperLimitFormat().getData() << ","
                        << pMpr->getTestResultScale().getData() << ","
                        << pMpr->getTestLowerLimitScale().getData() << ","
                        << pMpr->getTestUpperLimitScale().getData() << ","
                        << pMpr->getLowerSpecLimit().getData() << ","
                        << pMpr->getUpperSpecLimit().getData() << "endl";
            }
            break;

        case MRR:
            {
                //lot_id,wafer_id,recseq_no,finish_t,
                Mrr* pMrr = dynamic_cast<Mrr*>(rec);
                *pfile << lot_id <<","
                        << wafer_id <<","
                        << pMrr->getRecOrder() << ","
                        << Util::convertDtToMysqlDtFormat(pMrr->getFinishTime().getData()) << ","
                        //disp_cod,usr_desc,exc_desc,time_stamp
                        << pMrr->getDispCode().getData() << ","
                        << pMrr->getUserDescription().getData() << ","
                        << pMrr->getExecDescription().getData()
                       // << Util::convertDtToMysqlDtFormat(time(0))
                        << endl;
            }
            break;

        case PCR:
            {
                Pcr* pPcr = dynamic_cast<Pcr*>(rec);
                *pfile<< pPcr->getHeadNum().getData() << ","
                        << pPcr->getSiteNum().getData() << ","
                        << pPcr->getPartCount().getData() << ","
                        << pPcr->getAbortCount().getData() << ","
                        << pPcr->getRetestCount().getData() << ","
                        << pPcr->getGoodCount().getData() << ","
                        << pPcr->getFunctionalCount().getData() << endl;
            }
            break;

        case PGR:
            {
                Pgr* pPgr = dynamic_cast<Pgr*>(rec);
                *pfile << pPgr->getGroupIndex().getData() << ","
                        << pPgr->getGroupName().getData() << ","
                        << pPgr->getIndexCount().getData()
                        << convertUIntArraytoStr( pPgr->getPinIndexArray() )
                        << endl;
            }
            break;

        case PIR:
            {
                Pir* pPir = dynamic_cast<Pir*>(rec);
                *pfile << pPir->getHeadNum().getData() << ","
                        << pPir->getSiteNum().getData() << endl;
            }
            break;

        case PLR:
            {
                Plr* pPlr = dynamic_cast<Plr*>(rec);
                *pfile << pPlr->getGroupCount().getData() << ","
                        << convertUIntArraytoStr(pPlr->getGroupIndex() ) << ","
                        << convertUIntArraytoStr( pPlr->getGroupMode() ) << ","
                        << convertUIntArraytoStr( pPlr->getGroupDisplayRadix() ) << ","
                        << convertStrArraytoStr(pPlr->getProgramStateEncodeChars() ) << ","
                        << convertStrArraytoStr( pPlr->getReturnStateStateEncodeChars() ) << ","
                        << convertStrArraytoStr( pPlr->getProgramStateEncodeChars() ) << ","
                        << convertStrArraytoStr(pPlr->getReturnStateStateEncodeCharsL())
                        << endl;
            }
            break;

        case PMR:
            {
                Pmr* p = dynamic_cast<Pmr*>(rec);
                *pfile << p->getSiteNum().getData() << ","
                        << p->getUniquePinIndex().getData() << ","
                        << p->getChannelType().getData() << ","
                        << p->getChannelName().getData() << ","
                        << p->getPhysicalPinName().getData() << ","
                        << p->getLogicalPinName().getData() << ","
                        << p->getHeadNum().getData() << ","<<endl;
            }
            break;

        case PRR:
            {

                Prr* pPrr = dynamic_cast<Prr*>(rec);
                //"PRR: lot_id,wafer_id,recseq_no,/*test_cnt,*/"
                *pfile << lot_id <<","
                        << wafer_id <<","
                        << pPrr->getRecOrder() << ","
                        << pPrr->getSeqId() << ","
                        //"head_num,site_num,part_id,num_test,"
                        << pPrr->getHeadNumber().getData() << ","
                        << pPrr->getSiteNumber().getData() << ","
                        << pPrr->getPartId().getData() << ","
                        << pPrr->getNumberOfTests().getData() << ","
                        //"part_flg,hard_bin,soft_bin,x_coord,y_coord,"
                        << convertBoolArraytoStr( pPrr->getPartFlag() ) << ","
                        << pPrr->getHardBin().getData() << ","
                        << pPrr->getSoftBin().getData() << ","
                        << pPrr->getXCoordinate().getData() << ","
                        << pPrr->getYCoordinate().getData() <<","
                        //"rtst_cod,abrt_cod,test_t,part_txt,part_fix ","stamp_time"
                        // rtst_code  --> ?
                        //abrt_cod --> ?
                        << ","
                        << ","
                        << pPrr->getTestTime().getData() << ","
                        << pPrr->getPartText().getData() << ","
                        << pPrr->getPartFix().getData().toHex()
                        //<< Util::convertDtToMysqlDtFormat(time(0))
                        << endl;
            }
            break;

        case PTR:
            {
                PtrXY* p = dynamic_cast<PtrXY*>(rec);
                        //"PTR: lot_id,wafer_id,recseq_no,/*test_cnt,*/ "
                *pfile << lot_id <<","
                        << wafer_id <<","
                        << p->getRecOrder() << ","
                        << p->getSeqId() << ","

                        //"x_coord,y_coord,test_num,head_num,site_num,"
                        <<p->getXCoordinate().getData() << ","
                        << p->getYCoordinate().getData() << ","
                        <<p->getTestNumber().getData() << ","
                        << p->getHeadNumber().getData() << ","
                        << p->getSiteNumber().getData() << ","

                        //"test_flag,parm_flag,result,test_txt,alarm_text,"
                        << convertBoolArraytoStr( p->getTestFlag() ) << ","
                        << convertBoolArraytoStr( p->getParaTestFlag() ) << ","
                        << p->getResult().getData() << ","
                        << p->getTestText().getData() << ","
                        << p->getAlarmId().getData() << ","

                        //"opt_flag,res_scal,llm_scal,hlm_scal,lo_limit,hi_limit,time_stamp"
                        << convertBoolArraytoStr(p->getOptionFlag())<< ","
                        << p->getResultScale().getData() << ","
                        << p->getLowerLimitScale().getData() << ","
                        << p->getUpperLimitScale().getData() << ","
                        << p->getLowerTestLimitValue().getData() << ","
                        << p->getUpperTestLimitValue().getData() << ","

                        //"units,c_resfmt,c_llmfmt,c_hlmfmt,lo_spec,hi_spec"
                        << p->getResultsUnit().getData() << ","
                        // This causes binary output which can not be imported to MySQL
                        //<< p->getResultFormat().getData() << ","
                        << ","
                        //<< p->getLowerLimitScaleFormat().getData() << ","
                        << ","
                        //<< p->getUpperLimitScaleFormat().getData() << ","
                        << ","
                        //<< p->getLowerLimitSpec().getData() << ","
                        << ","
                        //<< p->getUpperLimitSpec().getData() << ","
                        //<< Util::convertDtToMysqlDtFormat(time(0))
                        <<endl;
            }
            break;

        case RDR:
            {
                Rdr* pRdr = dynamic_cast<Rdr*>(rec);
                *pfile<< pRdr->getRetestBinCount().getData() << ","
                << this->convertUIntArraytoStr(pRdr->getRetestBinNumber())<< endl;
            }
            break;

        case SBR:
            {                  
                Sbr* pSbr = dynamic_cast<Sbr*>(rec);
                //lot_id,wafer_id,recseq_no,
                *pfile << lot_id <<","
                    << wafer_id <<","
                    << pSbr->getRecOrder() << ","

                    //"head_num,site_num,bin_num,"
                    << pSbr->getHeadNum().getData() << ","
                    << pSbr->getSiteNum().getData() << ","
                    << pSbr->getSbinNum().getData() << ","

                    //"bin_cnt,bin_flg,bin_nam"
                    << pSbr->getSbinCount().getData() << ","
                    << pSbr->getSbinPassFail().getData() << ","
                    << pSbr->getSbinName().getData()
                    //<< Util::convertDtToMysqlDtFormat(time(0))
                    << endl;
            }
            break;

        case SDR:
            {

                Sdr* pSdr = dynamic_cast<Sdr*>(rec);
                //"lot_id,wafer_id,/*filesrc_id,*/recseq_no,"
                *pfile << lot_id <<","
                    << wafer_id <<","
                    << pSdr->getRecOrder() << ","
                //"head_num,site_grp,site_num,hand_typ,hand_id,card_typ,card_id"
                    << pSdr->getHeadNum().getData() << ","
                    << pSdr->getSiteGroup().getData() << ","
                    << convertUIntArraytoStr( pSdr->getSiteNum() ) << ","
                    << pSdr->getHandlerType().getData() << ","
                    << pSdr->getHandlerId().getData() << ","
                    << pSdr->getCableType().getData() << ","
                    << pSdr->getCableId().getData() << ","

                    //"load_typ,load_id,dib_typ,dib_id,cabl_tp,cabl_id,cont_typ,cont_id"
                    << pSdr->getLoadBoardType().getData() << ","
                    << pSdr->getLoadBoardId().getData() << ","
                    << pSdr->getDibBoardType().getData() << ","
                    << pSdr->getDibBoardId().getData() << ","
                    << pSdr->getProbeCardType().getData() << ","
                    << pSdr->getProbeCardId().getData() << ","
                    << pSdr->getContactorType().getData() << ","
                    << pSdr->getContactorId().getData() << ","

                    //<<"lasr_typ,lasr_id,extr_typ,extr_id"
                    << pSdr->getLaserType().getData() << ","
                    << pSdr->getLaserId().getData() << ","
                    << pSdr->getExtraEqptType().getData() << ","
                    << pSdr->getExtraEqptId().getData()
                    //<< Util::convertDtToMysqlDtFormat(time(0))
                    <<endl;
            }
            break;

        case TSR:
            {
                Tsr* pTsr = dynamic_cast<Tsr*>(rec);
                *pfile << pTsr->getHeadNumber().getData() << ","
                        << pTsr->getSiteNumber().getData() << ","
                        << pTsr->getTestType().getData() << ","
                        << pTsr->getTestNumber().getData() << ","
                        << pTsr->getExecutionCount().getData() << ","
                        << pTsr->getFailureCount().getData() << ","
                        << pTsr->getAlarmCount().getData() << ","
                        << pTsr->getTestName().getData() << ","
                        << pTsr->getSequenceName().getData() << ","
                        << pTsr->getTestLabel().getData() << ","
                        << convertBoolArraytoStr(pTsr->getOptionFlag()) << ","
                        << pTsr->getTestTime().getData() << ","
                        << pTsr->getLowestTestValue().getData() << ","
                        << pTsr->getHighestTestValue().getData() << ","
                        << pTsr->getTestResultSumValue().getData() << ","
                        << pTsr->getTestResultSquareValue().getData()
                        << endl;
            }
            break;

        case WCR:
            {
                Wcr* pWcr = dynamic_cast<Wcr*>(rec);
                *pfile << pWcr->getWaferSize().getData() << ","
                        << pWcr->getDieHeight().getData() << ","
                        << pWcr->getDieWidth().getData() << ","
                        << pWcr->getWaferUnit().getData() << ","
                        << pWcr->getWaferFlat().getData() << ","
                        << pWcr->getCenterX().getData() << ","
                        << pWcr->getCenterY().getData() << ","
                        << pWcr->getPositionX().getData() << ","
                        << pWcr->getPositionY().getData()
                        << endl;
            }
            break;

        case WIR:
            {
                //"WIR: lot_id,recseq_no,head_num,
                Wir* p = dynamic_cast<Wir*>(rec);
                *pfile  << lot_id <<","
                        << p->getRecOrder() << ","
                        << ","
                        << p->getHeadNum().getData() << ","
                        //site_num,start_t,site_grp,wafer_id"
                        << ","
                        << Util::convertDtToMysqlDtFormat( p->getStartTime().getData() ) << ","
                        << p->getSiteGroup().getData() << ","
                        << p->getWaferId().getData()
                        //<< Util::convertDtToMysqlDtFormat(time(0))
                        << endl;
            }
            break;

        case WRR:
            {
                Wrr* pWrr = dynamic_cast<Wrr*>(rec);
                *pfile << pWrr->getHeadNum().getData() << ","
                        << pWrr->getSiteGroup().getData() << ","
                        << pWrr->getPartCount().getData() << ","
                        << Util::convertDtToMysqlDtFormat( pWrr->getFinishTime().getData() ) << ","
                        << pWrr->getRetestCount().getData() << ","
                        << pWrr->getAbortCount().getData() << ","
                        << pWrr->getGoodCount().getData() << ","
                        << pWrr->getFunctionalCount().getData() << ","
                        << pWrr->getWaferId().getData() << ","
                        << pWrr->getFabWaferId().getData() << ","
                        << pWrr->getFrameId().getData() << ","
                        << pWrr->getMaskId().getData() << ","
                        << pWrr->getUserDescription().getData() << ","
                        << pWrr->getExecDescription().getData() << endl;
            }
            break;

        case UNK:
            // do nothing
            break;

        default:
            break;

        }
    }
}

void FilePrintDetail::writeRecAttributes(RecBase* rec){
    stdf_rec rt = rec->getStdfRec();
    switch (rt) {

    case ATR:
        (*pfile)<<"asctime,cmd_line"<<endl;
        break;

    case BPS:
        (*pfile)<<"recseq_no,order_index"<<endl;
        break;

    case DTR:
        (*pfile)<<"text_dat"<<endl;
        break;

    case EPS:
        (*pfile) <<"order_index"<<endl;
        break;

    case FAR:
        (*pfile) <<"STDF Version"<<endl;
        break;
    case FTR:
        (*pfile) << ""<< "test_num ," << "head_num," << "site_num," //<< p->test_flag,";
                << "vect_nam," << "time_set," << "cycl_cnt" << "rel_vadr," << "rept_cnt,"
                << "num_fail," << "xfail_ad," << "yfail_ad," << "vect_off,"
                //<<"rtn_indx,"
                //<< "rtn_stat,"
                //<< "pgm_indx,"
                //<< "pgm_stat,"
                << "fail_pin," << "op_code," << "test_txt," << "alrm_id," << "prog_txt,"
                << "rslt_txt," << "patg_num," << "spin_map"
                << endl;
        break;

    case GDR:
        (*pfile) <<""<<endl;
        break;

    case HBR: //**//
        (*pfile) << "lot_id,wafer_id,recseq_no,head_num,hbin_num,hbin_cnt,hbin_pf,hbin_nam" << endl;
        break;

    case MIR: //**//
        (*pfile) << "lot_id,wafer_id,part_typ,job_nam,node_nam,tstr_typ,setup_t,start_t,"
                 << "oper_nam,mode_cod,stat_num,sblot_id,test_cod,rtst_cod,job_rev,"
                 << "exec_typ,exec_ver,prot_cod,cmod_cod,burn_tim,tst_temp,user_txt,"
                 << "aux_file,pkg_typ,famly_id,dat_cod,facil_id,floor_id,proc_id,oper_frq,"
                 << "spec_nam,spec_ver,flow_id,setup_id,dsgn_rev,eng_id,rom_cod,serl_num,supr_nam"
                 << endl;
        break;

    case MPR:
        (*pfile) << "" << " test_num.getData(),"
                << "head_num," << "site_num,"
                //file << pMpr->rtn_stat.getData() << ",";
                //file << pMpr->rtn_rslt.getData() << ",";
                << "units," << "lo_limit," << "hi_limit,"
                << "start_in," << "incr_in," << "units_in,"
                //file << pMpr->rtn_indx.getData() << ","
                << "c_resfmt," << "c_llmfmt," << "c_hlmfmt,"
                << "res_scal,"<< "llm_scal,"<< "hlm_scal,"
                << "lo_spec," << "hi_spec," << endl;
        break;

    case MRR: //**//
        (*pfile) << "lot_id,wafer_id,recseq_no,finish_t,disp_cod,usr_desc,exc_desc" << endl;
        break;

    case PCR:
        (*pfile) << "head_num,"
                << "site_num," << "part_cnt," << "abrt_cnt,"
                << "rtst_cnt," << "good_cnt," << "func_cnt" << endl;
        break;

    case PGR:
        (*pfile) << "grp_indx," << "grp_nam," << "indx_cnt," << "pmr_indx" << endl;
        break;

    case PIR:
        (*pfile) << "head_num," << "site_num " << endl;
        break;

    case PLR:
        (*pfile) << "grp_cnt," << "rp_indx," << "grp_mode," << "grp_radx,"
                << "pgm_char,"<< "rtn_char," << "pgm_chal,"<< "rtn_chal" << endl;
        break;

    case PMR:
        (*pfile) << "site_num,"<< "pmr_indx," << "chan_typ," << "chan_nam,"
                << "phy_nam," << "log_nam," << "head_num" << endl;
        break;

    case PRR: //**//
        (*pfile) << "lot_id,wafer_id,recseq_no,test_cnt,"
                 <<"head_num,site_num,part_id,num_test,"
                 <<"part_flg,hard_bin,soft_bin,x_coord,y_coord,"
                 <<"rtst_cod,abrt_cod,test_t,part_txt,part_fix "
                 << endl;
        break;
    case PTR: //**//
        (*pfile) << "lot_id,wafer_id,recseq_no,test_cnt,"
                << "x_coord,y_coord,test_num,head_num,site_num,"
                << "test_flag,parm_flag,result,test_txt,alarm_txt,"
                << "opt_flag,res_scal,llm_scal,hlm_scal,lo_limit,hi_limit,"
                << "units,c_resfmt,c_llmfmt,c_hlmfmt,lo_spec,hi_spec"
                << endl;
        break;

    case RDR:
        (*pfile) << "num_bins,tst_bin"<< endl;
        break;

    case SBR: //**//

        (*pfile) << "lot_id,wafer_id,recseq_no,"
                 <<"head_num,site_num,bin_num,"
                 <<"bin_cnt,bin_flg,bin_nam"
                 << endl;
        break;

    case SDR: //**//
        (*pfile)<<"lot_id,wafer_id,recseq_no,"
        <<"head_num,site_grp,site_num,hand_typ,hand_id,card_typ,card_id,"
        <<"load_typ,load_id,dib_typ,dib_id,cabl_tp,cabl_id,cont_typ,cont_id,"
        <<"lasr_typ,lasr_id,extr_typ,extr_id"
        << endl;
        break;

    case TSR:
        (*pfile) << "head_num," << "site_num," << "test_typ,"
                << "test_num," << "exec_cnt," << "fail_cnt,"
                << "alrm_cnt," << "test_nam," << "seq_nam,"
                << "test_lbl," //<< "opt_flag,"
                << "test_tim," << "test_min,"<< "test_max,"
                << "test_sum," << "test_sqrs"<< endl;
        break;

    case WCR:
        (*pfile) << "wfr_siz,"
                << "die_ht,"
                << "die_wid,"
                << "wfr_unit,"
                << "wfr_flat,"
                << "center_x,"
                << "center_y,"
                << "pos_x,"
                << "pos_y"
                << endl;
        break;

    case WIR: //**//
        (*pfile) << "lot_id,recseq_no,head_num,site_num,start_t,site_grp,wafer_id" << endl;
        break;

    case WRR:
        *pfile << "head_num,"
                << "site_grp,"
                << "part_cnt,"
                << "finish_t,"
                << "rtst_cnt,"
                << "abrt_cnt,"
                << "good_cnt,"
                << "func_cnt,"
                << "wafer_id,"
                << "fabwf_id,"
                << "frame_id,"
                << "mask_id,"
                << "usr_desc,"
                << "excr_desc" << endl;
        break;

    case UNK:
        // do nothing
        break;

    default:
        break;

    }
}

void FilePrintDetail::setWaferID(const string val){
    wafer_id = val;
}

void FilePrintDetail::setLotID(const string val){
    lot_id = val;
}

string FilePrintDetail::convertBoolArraytoStr(StdfField<vector<bool> > flag){
    string str;
    vector<bool> flagVec = flag.getData();
    const int sz = flagVec.size();
    for( int i=0; i<sz; ++i) {

        if(flagVec[i] == true){
            str.push_back('1');
        }
        else {
            str.push_back('0');
        }
    }
    return str;
}

string FilePrintDetail::convertUIntArraytoStr(StdfField<vector<unsigned int> > uintVals){
    stringstream oss;
    char sep = '|';
    vector<unsigned int> uintVec = uintVals.getData();
    const int sz = uintVec.size();
    for( int i=0; i<sz; ++i) {
        oss<<uintVec[i];
        if(i<sz-1)  oss<<sep;
    }
    return oss.str();
}

string FilePrintDetail::convertIntArraytoStr(StdfField<vector<int> > uintVals){
    stringstream oss;
    char sep = '|';
    vector<int> uintVec = uintVals.getData();
    const int sz = uintVec.size();
    for( int i=0; i<sz; ++i) {
        oss<<uintVec[i];
        if(i<sz-1)  oss<<sep;
    }
    return oss.str();
}

string FilePrintDetail::convertStrArraytoStr(StdfField<vector<string> > uintVals){
    stringstream oss;
    char sep = '|';
    vector<string> uintVec = uintVals.getData();
    const int sz = uintVec.size();
    for( int i=0; i<sz; ++i) {
        oss<<uintVec[i];
        if(i<sz-1)  oss<<sep;
    }
    return oss.str();
}

bool FilePrintDetail::saveToFile(std::vector<RecBase*> vec,std::string filename)
{
    if(filename.empty() || vec.empty() )
        return false;

    if( pfile == NULL){
        pfile = new std::ofstream;
    }
    if( !pfile->is_open() )
    {
        pfile->open(filename.c_str());
    }
    writeRecAttributes(*vec.begin());
    for( std::vector<RecBase*>::iterator it= vec.begin(); it != vec.end(); ++it )
    {
        out(*it);
    }

    pfile->close();
    delete pfile;
    pfile = NULL;
    return true;
}

void FilePrintDetail::savePtrRecord(std::vector<RecBase*> recVec){
    string str = "ptr";
    std::vector<RecBase*> storeRecVec;
    std::vector< int >::iterator it_tn;
    std::vector<RecBase*>::iterator it_rec;
    std::vector< int >testNumVec = StdfConfig::instance()->getTestNumbersSetting();
    mkdir( str.c_str(),0777);
    chdir( str.c_str() );
    cout << "Created PTR folder!" << endl;
    cout << "Printing recVec " << recVec.size() << " PTR records..." << endl;
    cout << "Printing testNumVec " << testNumVec.size() << " PTR records..." << endl;

    for( it_tn = testNumVec.begin(); it_tn != testNumVec.end(); ++it_tn){
        const long test_num = static_cast<long>(*it_tn);
        storeRecVec.clear();
        for(it_rec= recVec.begin();it_rec!=recVec.end(); ++it_rec){
             Ptr* pPtr = dynamic_cast<Ptr*>(*it_rec);
             if( pPtr->getTestNumber().getData() == test_num ){
                 storeRecVec.push_back(pPtr);
             }
        }
        stringstream oss;
        oss << "ptr_"<<test_num;
        oss<<".txt";
        cout << "Printed to ptr_" << test_num << ".txt" << endl;
        str = oss.str();
        cout<<str<<endl;
        saveToFile(storeRecVec,str.c_str());
    }
}
